#!/bin/bash

# 缺失的定义
pdir=$(pwd)
srctree=$pdir/linux-custom
SRCARCH=arm64
KCONFIG_CONFIG=.config

# 来源 linux/scripts/builddeb 的片段

is_enabled() {
	grep -q "^$1=y" include/config/auto.conf
}

cd $srctree

(
	find . arch/$SRCARCH -maxdepth 1 -name Makefile\*
	find include scripts -type f -o -type l
	find arch/$SRCARCH -name Kbuild.platforms -o -name Platform
	find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
) > hdrsrcfiles

(		
	if is_enabled CONFIG_STACK_VALIDATION; then
		echo tools/objtool/objtool
	fi

	find arch/$SRCARCH/include Module.symvers include scripts -type f

	if is_enabled CONFIG_GCC_PLUGINS; then
		find scripts/gcc-plugins -name \*.so
	fi
) > hdrobjfiles

version=$(cat include/config/kernel.release)

destdir=$pdir/linux-headers-$version
mkdir -p $destdir
tar -c -f - -C $srctree -T hdrsrcfiles | tar -xf - -C $destdir
tar -c -f - -T hdrobjfiles | tar -xf - -C $destdir
rm -f hdrsrcfiles hdrobjfiles

# copy .config manually to be where it's expected to be
cp $KCONFIG_CONFIG $destdir/.config


